package com.runshu.newsmartark.smartbox;

import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import androidx.annotation.Nullable;

import com.runshu.newsmartark.base.BoxBaseActivity;

import java.util.ArrayDeque;
import java.util.Queue;

public class SmartBoxIntentService extends IntentService {
    public static String ACTION = "action";
    public static String DELAY = "delay";
    public static final int ACTION_OPEN_DOOR = 10;
    public static final int ACTION_OPEN_LIGHT = 11;
    public static final int ACTION_CLOSE_LIGHT = 12;
    public static final int ACTION_START_SCAN = 13;
    public static final int ACTION_END_SCAN = 14;
    public static final int ACTION_CLOSE_DOOR = 15;
    public static final int ACTION_START_SCAN_A = 16;

    public static Queue<Intent> services = new ArrayDeque<>();
    public static Thread thread;
    public static void operateBox(int action, int delay, Context ctx) {
        Intent service = new Intent( ctx, SmartBoxIntentService.class );
        service.putExtra( ACTION, action );
        service.putExtra( DELAY, delay );
        BoxBaseActivity.ctx.startService( service );

    }

    /**
     * Creates an IntentService.  Invoked by your subclass's constructor.
     */
    public SmartBoxIntentService() {
        super( "box_service" );
        try {
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    protected void onHandleIntent(@Nullable Intent intent) {

        int commend = intent.getIntExtra( ACTION, 0 );
        int delay = intent.getIntExtra( DELAY, 0 );
        if (delay != 0) {
            sleep( delay );
        }
        if (commend == 0) return;
        Log.e("box_commend", commend+"");
        switch (commend) {
            case ACTION_OPEN_DOOR:
                openDoor();
                break;
            case ACTION_CLOSE_LIGHT:
                closeLigth();
                break;
            case ACTION_OPEN_LIGHT:
                openLight();
                break;
            case ACTION_CLOSE_DOOR:
                closeDoor();
                break;
            case ACTION_END_SCAN:
//                sleep( 5 );
//                BoxBaseActivity.ctx.stopInventory();
                break;
            case ACTION_START_SCAN:
                sleep( 5 );
                BoxBaseActivity.ctx.startInventory();
                break;
            case ACTION_START_SCAN_A:
                sleep( 5 );
                if (TarGet.getCurrentTarget() == 1) {
                    BoxBaseActivity.ctx.startInventory();
                } else {
//                    BoxBaseActivity.ctx.stopInventory();
                }
                break;
        }
    }

    private void sleep(int delay) {
        try {
            Thread.sleep( delay );
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private void openLight() {
        try {
            Log.e( "box", "openlight" );
            Thread.sleep( 100 );
//            BoxBaseActivity.ctx.SetLightStatus(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    //开门
    private void openDoor() {
        try {
            Log.e( "box", "openDoor" );
            Thread.sleep( 100 );
            BoxBaseActivity.ctx.SetDoorStatus( 0);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    //关门
    public void closeDoor() {
        try {
            Log.e( "box", "closeDoor" );

            Thread.sleep( 100 );
//            BoxBaseActivity.ctx.SetDoorStatus( 0);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    //关灯
    public void closeLigth() {
        try {
            Log.e( "box", "closeLight" );

            Thread.sleep( 100 );
//            BoxBaseActivity.ctx.SetDoorStatus(0);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


}
